package com.itheima.utils;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.IOException;
import java.io.InputStream;

public class sessionUtils {
      private static SqlSessionFactory factory = null;
      private static SqlSession session = null;
      private static InputStream is = null;

    public static SqlSession getSession() {
        try {
            is = Resources.getResourceAsStream("sql.xml");
            factory = new SqlSessionFactoryBuilder().build(is);
            session = factory.openSession();
        } catch (IOException e) {
            e.printStackTrace();
        }
         return session;
    }

    public static void closs() {
        session.commit();
        if (session != null) {
            session.close();
        }
        if (is != null){
            try {
                is.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
